package org.unicen.exa.io.worker.assigner.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.unicen.exa.io.worker.assigner.core.model.Job;
import org.unicen.exa.io.worker.assigner.dao.JobDao;

@Repository("jobDao")
public class JobDaoImpl extends BaseDaoImpl<Job> implements JobDao<Job>{
	
	private Logger logger = Logger.getLogger(JobDaoImpl.class);
	private Map<Long, Job> map = new HashMap<Long, Job>();
	

	public void post(Long id, Job model) {
//		logger.info(String.format("STORING job with id equal to %s", id.toString()));
		map.put(id, model);
	}

	public void put(Job model) {
//		logger.info(String.format("MERGING job with id equal to %s", model.getId().toString()));
		map.put(model.getId(), model);
		
		
	}

	public void delete(Job model) {
//		logger.info(String.format("REMOVING job with id equal to %s", model.getId().toString()));
		map.remove(model.getId());
	}

	public Job get(Long id) {
//		logger.info(String.format("GETTING job with id equal to %s", id.toString()));
		return map.get(id);
	}

	public synchronized List<Job> gets() {
//		logger.info(String.format("GETTING all jobs"));
		List<Job> list = new ArrayList<Job>();
		for (Entry<Long, Job> iterable_element : map.entrySet()) {
			list.add(iterable_element.getValue());
		}
		
		return list;
	}

	public Integer countJobsByState(String jobState) {
		logger.info(String.format("COUNTING jobs with state in %s", jobState));
		Integer count = new Integer(0);
		for (Entry<Long, Job> iterable_element : map.entrySet()) {
			if (iterable_element.getValue().getState().equals(jobState)){
				count++;
			}
		}
		logger.info(String.format("Amount  jobs with state in %s equals to %s", jobState, count.toString()));
		return count;
	}


}
